package com.shop.servlet;

import com.shop.bean.User;
import com.shop.dao.UserDao;
import com.shop.dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "InsertServlet" ,urlPatterns = "/zhang.do")
public class InsertServlet extends HttpServlet {
    UserDao userDao = new UserDaoImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        User user1 = userDao.queryUserByUsername(username);
        if (user1 == null) {
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            User user = new User(username, password, email);
            userDao.insert(user);
            response.sendRedirect("http://localhost:8080/pages/user/regist_success.html");
        }else {
            response.sendRedirect("http://localhost:8080/pages/user/regist.html");
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }
}
